package com.tyk.demo.utils;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;

@Component
public class FTPFileUploadUtils {
//    private static final String FTP_ADDRESS = "127.0.0.1";
    private static final String FTP_ADDRESS = "47.105.54.191";
    private static final int FTP_PORT  = 21;
    private static final String FTP_USERNAME  = "ftptest";
    private static final String FTP_PASSWORD  = "paw123###";
    private static final String FTP_BASEPATH  = "/home/wwwroot/ftptest";

    public static boolean uploadFile(String originalFileName, InputStream inputStream) {
        boolean isConnected = false;
        FTPClient client = new FTPClient();
        client.setControlEncoding("GBK");
        try {
            int reply;
            client.connect(FTP_ADDRESS,FTP_PORT);
            client.login(FTP_USERNAME, FTP_PASSWORD);
            reply = client.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                client.disconnect();
                return isConnected;
            }
            client.setFileType(FTPClient.BINARY_FILE_TYPE);
//            client.makeDirectory(FTP_BASEPATH);
            client.changeWorkingDirectory(FTP_BASEPATH);
            // 上传文件
            client.storeFile(originalFileName, inputStream);
            // 下载文件
//            client.retrieveFileStream(originalFileName);
            inputStream.close();
            client.logout();
            isConnected = true;
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (client.isConnected()) {
                try {
                    client.disconnect();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return isConnected;
    }

    public static InputStream downloadFile(String fileName) {
        FTPClient client = new FTPClient();
        client.setControlEncoding("GBK");
        InputStream inputStream = null;
        try {
            int reply;
            client.connect(FTP_ADDRESS,FTP_PORT);
            client.login(FTP_USERNAME, FTP_PASSWORD);
            reply = client.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                client.disconnect();
                throw new IOException("Fail to connect to FTP Server!!!");
            }
            client.setFileType(FTPClient.BINARY_FILE_TYPE);
            client.changeWorkingDirectory(FTP_BASEPATH);
            // 上传文件
//            client.storeFile(originalFileName, inputStream);
            // 下载文件
            inputStream = client.retrieveFileStream(fileName);
            return inputStream;
//            client.logout();
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (client.isConnected()) {
                try {
                    client.disconnect();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return inputStream;
    }

//        public String sdf() {
//        String server = (String) config.get("server");
//        Integer port = Integer.parseInt((String) config.get("port"));
//        String user = (String) config.get("user");
//        String password = (String) config.get("password");
//        String url_fileName = URLEncoder.encode(versionName, "GBK");
//        String url="ftp://"+user+":"+password+"@"+server+"/"+downLoadPath+"/"+url_fileName+"";
//        return url;
//    }
}
